package ee.maoinvest.frontend.controller.requestprocessors;

import java.sql.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.model.data.ElectricityContract;
import ee.maoinvest.backend.model.data.views.EnterpriseContractWithElectricityMeter;
import ee.maoinvest.frontend.controller.formstringdataconverters.electricitycontractforms.DeleteElectricityContractFormConverter;
import ee.maoinvest.frontend.controller.formstringdataconverters.electricitycontractforms.EditElectricityContractFormConverter;
import ee.maoinvest.frontend.controller.formstringdataconverters.electricitycontractforms.InsertElectricityContractFormConverter;

public class ElectricityContractFormsRequestProcessor 
{
	private final String EMPTY_STRING = "";
	private final int GET_EDITABLE_ELECTRICITY_CONTRACT_ID_PARSE_EXCEPTION_INDICATOR = -1;
	
	public InsertElectricityContractFormConverter fillInsertElectricityContractFormConverterWithRequestData(
			HttpServletRequest request)
	{
		InsertElectricityContractFormConverter insertElectricityContractFormConverter =
				new InsertElectricityContractFormConverter();
		
		String electricityContractName = request.getParameter("insertElectricityContractName");
		String ampereBasedFeePerA = request.getParameter("insertElectricityContractAmpereBasedFeePerA");
		String transmissionFeeDayRatePerkWh = request.getParameter("insertElectricityContractTransmissionFeeDayRatePerkWh");
		String transmissionFeeNightRatePerkWh = request.getParameter("insertElectricityContractTransmissionFeeNightRatePerkWh");
		String reactivePowerFeePerkvarh = request.getParameter("insertElectricityContractReactivePowerFeePerkvarh");
		String dateInserted = request.getParameter("insertElectricityContractDateInserted");
		String networkServiceRate = request.getParameter("networkServiceRate");
		String electricityContractTypeID = request.getParameter("insertElectricityContractContractTypeID");
		
		HttpSession session = request.getSession();
		Date latestDateAfterWhatDataManipulationIsPossible = (Date) session.getAttribute("latestDateAfterWhatDataManipulationIsPossible");
		
		insertElectricityContractFormConverter.setElectricityContractName(electricityContractName);
		insertElectricityContractFormConverter.setAmpereBasedFeePerA(ampereBasedFeePerA);
		insertElectricityContractFormConverter.setTransmissionFeeDayRatePerkWh(transmissionFeeDayRatePerkWh);
		insertElectricityContractFormConverter.setTransmissionFeeNightRatePerkWh(transmissionFeeNightRatePerkWh);
		insertElectricityContractFormConverter.setReactivePowerFeePerkvarh(reactivePowerFeePerkvarh);
		insertElectricityContractFormConverter.setDateInserted(dateInserted);
		insertElectricityContractFormConverter.setNetworkServiceRate(networkServiceRate);
		insertElectricityContractFormConverter.setElectricityContractTypeID(electricityContractTypeID);
		insertElectricityContractFormConverter.setLatestDateAfterWhatDataManipulationIsPossible(latestDateAfterWhatDataManipulationIsPossible);
		
		return insertElectricityContractFormConverter;
	}
	
	public DeleteElectricityContractFormConverter fillDeleteElectricityContractFormConverterWithRequestData(
			HttpServletRequest request)
	{
		DeleteElectricityContractFormConverter deleteElectricityContractFormConverter = 
				new DeleteElectricityContractFormConverter();
		
		String electricityContractIDThatIsChosenToDelete = request.getParameter("electricityContractIDThatIsChosenToDelete");
		
		HttpSession session = request.getSession();
		ElectricityContract electricityContractBeingDeleted = 
				(ElectricityContract) session.getAttribute("electricityContractBeingDeleted");
		@SuppressWarnings("unchecked")
		List<EnterpriseContractWithElectricityMeter> listOfAllContractRelationsWithElectricityMetersAndEnterprises = 
				(List<EnterpriseContractWithElectricityMeter>) session.getAttribute("listOfAllContractRelationsWithElectricityMetersAndEnterprises");
	
		deleteElectricityContractFormConverter.setElectricityContractIDThatIsChosenToDelete(electricityContractIDThatIsChosenToDelete);
		deleteElectricityContractFormConverter.setElectricityContractBeingDeleted(electricityContractBeingDeleted);
		deleteElectricityContractFormConverter.setListOfAllContractRelationsWithElectricityMetersAndEnterprises(
				listOfAllContractRelationsWithElectricityMetersAndEnterprises);
		
		return deleteElectricityContractFormConverter;
	}
	
	public EditElectricityContractFormConverter fillEditElectricityContractFormConverterWithRequestData(
			HttpServletRequest request)
	{
		EditElectricityContractFormConverter editElectricityContractFormConverter =
				new EditElectricityContractFormConverter();
		
		//getting data from form, that has been put on request
		String electricityContractIDToEdit = request.getParameter("electricityContractIDToEdit");
		String electricityContractName = request.getParameter("electricityContractName");
		String ampereBasedFeePerA = request.getParameter("ampereBasedFeePerA");
		String transmissionFeeDayRatePerkWh = request.getParameter("transmissionFeeDayRatePerkWh");
		String transmissionFeeNightRatePerkWh = request.getParameter("transmissionFeeNightRatePerkWh");
		String reactivePowerFeePerkvarh = request.getParameter("reactivePowerFeePerkvarh");
		String dateInserted = request.getParameter("dateInserted");
		String networkServiceRate = request.getParameter("networkServiceRate");

		editElectricityContractFormConverter.setElectricityContractIDToEdit(electricityContractIDToEdit);
		editElectricityContractFormConverter.setElectricityContractName(electricityContractName);
		editElectricityContractFormConverter.setAmpereBasedFeePerA(ampereBasedFeePerA);
		editElectricityContractFormConverter.setTransmissionFeeDayRatePerkWh(transmissionFeeDayRatePerkWh);
		editElectricityContractFormConverter.setTransmissionFeeNightRatePerkWh(transmissionFeeNightRatePerkWh);
		editElectricityContractFormConverter.setReactivePowerFeePerkvarh(reactivePowerFeePerkvarh);
		editElectricityContractFormConverter.setDateInserted(dateInserted);
		editElectricityContractFormConverter.setNetworkServiceRate(networkServiceRate);
		
		return editElectricityContractFormConverter;
	}
	
	public int getEditableElectricityContractID(HttpServletRequest request)
	{
		int stringParsedToInt = GET_EDITABLE_ELECTRICITY_CONTRACT_ID_PARSE_EXCEPTION_INDICATOR;
		
		String electricityContractID = request.getParameter("electricityContractIDToEditForm");
		if (electricityContractID != null && !electricityContractID.equals(EMPTY_STRING))
		{
			try 
			{
				stringParsedToInt = Integer.parseInt(electricityContractID);
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				return GET_EDITABLE_ELECTRICITY_CONTRACT_ID_PARSE_EXCEPTION_INDICATOR;
			}
		}
		
		return stringParsedToInt;
	}
	
	public int getElectricityContractIDToDelete(HttpServletRequest request)
	{
		int stringParsedToInt = GET_EDITABLE_ELECTRICITY_CONTRACT_ID_PARSE_EXCEPTION_INDICATOR;
		
		String enterpriseID = request.getParameter("electricityContractIDToDelete");
		if (enterpriseID != null && !enterpriseID.equals(EMPTY_STRING))
		{
			try 
			{
				stringParsedToInt = Integer.parseInt(enterpriseID);
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				return GET_EDITABLE_ELECTRICITY_CONTRACT_ID_PARSE_EXCEPTION_INDICATOR;
			}
		}
		
		return stringParsedToInt;
	}

}
